<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="支付超时">
              <el-input v-model.trim="postForm.order_pay_timeout"/>
              <span>单位: 分钟</span>
            </el-form-item>
            <el-form-item label="收货超时">
              <el-input v-model.trim="postForm.order_received_timeout"/>
              <span>单位: 天, 发货开始算</span>
            </el-form-item>
            <el-form-item label="商家支付" v-if="false">
              <el-input v-model.trim="postForm.mini_merchant_pay"/>
              <span>线下商家最低支付金额</span>
            </el-form-item>
            <el-form-item label="提现次数">
              <el-input v-model.trim="postForm.cash_total_for_day"/>
              <span>次/天</span>
            </el-form-item>
            <el-form-item label="提现最小金额">
              <el-input v-model.trim="postForm.cash_min_money"/>
              <span>如: 100</span>
            </el-form-item>
            <el-form-item label="提现服务费">
              <el-input v-model.trim="postForm.cash_service"/>
              <span>单位: %</span>
            </el-form-item>
            <el-form-item label="客服电话">
              <el-input v-model.trim="postForm.service_mobile"/>
            </el-form-item>
            <el-form-item label="客服微信">
              <el-input v-model.trim="postForm.service_wx"/>
            </el-form-item>
            <el-form-item label="关于我们">
              <Tinymce
                ref="editor"
                v-model="postForm.about"
                :height="400"
                :quality="50"
                bucket="about"
                :image-max-width="750"
              />
            </el-form-item>
            <el-form-item label="注册协议" style="margin-bottom: 30px;">
              <Tinymce
                ref="editor"
                v-model="postForm.agreement"
                :height="400"
                :quality="50"
                bucket="agreement"
                :image-max-width="750"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import Tinymce from '@/components/Tinymce'
import api from '@/api'
export default {
  name: 'SystemApp',
  components: {
    Sticky,
    Tinymce
  },
  data() {
    return {
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const { data } = await api.system.fetchBasic()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
      this.postForm.show_system = +this.postForm.show_system === 1
      this.postForm.open_test = +this.postForm.open_test === 1
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          this.loading = true
          api.system.saveBasic({
            ...this.postForm,
            show_system: this.postForm.show_system ? 1 : 0,
            open_test: this.postForm.open_test ? 1 : 0
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
